package com.yupi.project.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
public class FileUtils {
    /**
     * 固定前缀名称
     */
    static String pre = "D:\\Projects\\IDEAProjects\\eruptDemo\\src\\main\\resources\\static\\";

    public static String upload(MultipartFile file) {
        if (file.isEmpty()) {
            return "图片上传失败";
        }
        //获取原文件名
        String originalFileName = file.getOriginalFilename();
        int pos = originalFileName.lastIndexOf('.');
        String UUID = java.util.UUID.randomUUID().toString().replace("-", "");
        String ext = originalFileName.substring(pos);
        String fileName = UUID + ext;
        String path = pre + fileName;
        try {
            file.transferTo(new File(path));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //这个就是插入到数据库的名称
        return fileName;
    }
}
